//
//  AlmasAlertView.m
//  DXAlertView
//
//  Created by Alimjan on 3/13/14.
//  Copyright (c) 2014 xiekw. All rights reserved.
//

#import "AlmasAlertView.h"

#import <QuartzCore/QuartzCore.h>


#define kAlertWidth self.frame.size.width
#define kAlertHeight self.frame.size.height

@interface AlmasAlertView ()
{
    
}

@property (nonatomic, strong)UIView *backImageView;

@end

@implementation AlmasAlertView

+(id )instanceAlertViewWithXibName:(NSString*)strXibName
{
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:strXibName owner:nil options:nil];
    return [nibView objectAtIndex:0];
}
+(id )instanceAlertView
{
    NSString *className = NSStringFromClass([self class]);
    
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:className owner:nil options:nil];
    
    return [nibView objectAtIndex:0];
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    if (self=[super initWithCoder:aDecoder]) {
        
    }
    return self;
}



- (void)show
{
    UIWindow *shareWindow = [UIApplication sharedApplication].keyWindow;
    self.frame = CGRectMake((CGRectGetWidth(shareWindow.bounds) - kAlertWidth) * 0.5, (CGRectGetHeight(shareWindow.bounds) - kAlertHeight) * 0.5, kAlertWidth, kAlertHeight);
    [shareWindow addSubview:self];
}
-(void)showAsActionSheet{
    UIWindow *shareWindow = [UIApplication sharedApplication].keyWindow;
    self.frame = CGRectMake(0, (CGRectGetHeight(shareWindow.bounds) - kAlertHeight), kAlertWidth, kAlertHeight);
    
    [shareWindow addSubview:self];
}
- (void)dismissAlert
{
    [self removeFromSuperview];
    if (self.dismissBlock) {
        self.dismissBlock();
    }
}
- (void)removeFromSuperview
{
    [self.backImageView removeFromSuperview];
    self.backImageView = nil;
    UIWindow *shareWindow = [UIApplication sharedApplication].keyWindow;
    CGRect afterFrame = CGRectMake((CGRectGetWidth(shareWindow.bounds) - kAlertWidth) * 0.5, (CGRectGetHeight(shareWindow.bounds) - kAlertHeight) * 0.5, kAlertWidth, kAlertHeight);
    
    [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.frame = afterFrame;
        self.alpha = 0.0f;
    } completion:^(BOOL finished) {
        [super removeFromSuperview];
    }];
}

- (void)willMoveToSuperview:(UIView *)newSuperview
{
    [super willMoveToSuperview:newSuperview];
    if (newSuperview == nil) {
        return;
    }
    UIWindow *shareWindow = [UIApplication sharedApplication].keyWindow;
    
    if (!self.backImageView) {
        
        CGRect imgFrame = CGRectMake(0, 0, 1000, 1000);
        self.backImageView = [[UIView alloc] initWithFrame:imgFrame];
    }
    UITapGestureRecognizer * g = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageClicked)];
    [self.backImageView addGestureRecognizer:g ];
    self.backImageView.backgroundColor = _backColor!=nil?_backColor:[UIColor blackColor];
    self.backImageView.alpha = 0.3f;
    [shareWindow addSubview:self.backImageView];
    [shareWindow clipsToBounds];
    self.alpha=0.1;
    
    [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.alpha = 1.0f;
    } completion:^(BOOL finished) {
    }];

}
-(void)imageClicked{
    if (!self.dontDissmissPressOutSide) {
        [self dismissAlert];
    }
    
}


@end
